#library a bunch of packages we may (or may not) use - install them first if not installed already. 
library(tidyverse)
## Warning: 程辑包'tidyverse'是用R版本4.1.2 来建造的
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.7
## v tidyr   1.1.4     v stringr 1.4.0
## v readr   2.1.1     v forcats 0.5.1
## Warning: 程辑包'ggplot2'是用R版本4.1.2 来建造的
## Warning: 程辑包'tibble'是用R版本4.1.2 来建造的
## Warning: 程辑包'tidyr'是用R版本4.1.2 来建造的
## Warning: 程辑包'readr'是用R版本4.1.2 来建造的
## Warning: 程辑包'dplyr'是用R版本4.1.2 来建造的
## Warning: 程辑包'stringr'是用R版本4.1.2 来建造的
## Warning: 程辑包'forcats'是用R版本4.1.2 来建造的
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(tmap)
## Warning: 程辑包'tmap'是用R版本4.1.2 来建造的
library(geojsonio)
## Warning: 程辑包'geojsonio'是用R版本4.1.2 来建造的
## Registered S3 method overwritten by 'geojsonsf':
##   method        from   
##   print.geojson geojson
## 
## 载入程辑包:'geojsonio'
## The following object is masked from 'package:base':
## 
##     pretty
library(plotly)
## Warning: 程辑包'plotly'是用R版本4.1.2 来建造的
## 
## 载入程辑包:'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(rgdal)
## Warning: 程辑包'rgdal'是用R版本4.1.2 来建造的
## 载入需要的程辑包:sp
## Warning: 程辑包'sp'是用R版本4.1.2 来建造的
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## 
## rgdal: version: 1.5-27, (SVN revision 1148)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
## Path to GDAL shared files: D:/R 4.1.1/library/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: D:/R 4.1.1/library/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-6
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
## Overwritten PROJ_LIB was D:/R 4.1.1/library/rgdal/proj
library(broom)
## Warning: 程辑包'broom'是用R版本4.1.2 来建造的
library(mapview)
## Warning: 程辑包'mapview'是用R版本4.1.2 来建造的
library(crosstalk)
## Warning: 程辑包'crosstalk'是用R版本4.1.2 来建造的
library(sf)
## Warning: 程辑包'sf'是用R版本4.1.2 来建造的
## Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1
library(sp)
library(spdep)
## Warning: 程辑包'spdep'是用R版本4.1.2 来建造的
## 载入需要的程辑包:spData
## Warning: 程辑包'spData'是用R版本4.1.2 来建造的
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
library(car)
## Warning: 程辑包'car'是用R版本4.1.2 来建造的
## 载入需要的程辑包:carData
## 
## 载入程辑包:'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following object is masked from 'package:purrr':
## 
##     some
library(fs)
library(janitor)
## Warning: 程辑包'janitor'是用R版本4.1.2 来建造的
## 
## 载入程辑包:'janitor'
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(ggplot2)
library(spatstat)
## Warning: 程辑包'spatstat'是用R版本4.1.2 来建造的
## 载入需要的程辑包:spatstat.data
## Warning: 程辑包'spatstat.data'是用R版本4.1.2 来建造的
## 载入需要的程辑包:spatstat.geom
## Warning: 程辑包'spatstat.geom'是用R版本4.1.2 来建造的
## Registered S3 method overwritten by 'spatstat.geom':
##   method     from
##   print.boxx cli
## spatstat.geom 2.3-1
## 
## 载入程辑包:'spatstat.geom'
## The following object is masked from 'package:car':
## 
##     ellipse
## 载入需要的程辑包:spatstat.core
## Warning: 程辑包'spatstat.core'是用R版本4.1.2 来建造的
## 载入需要的程辑包:nlme
## 
## 载入程辑包:'nlme'
## The following object is masked from 'package:dplyr':
## 
##     collapse
## 载入需要的程辑包:rpart
## spatstat.core 2.3-2
## 
## 载入程辑包:'spatstat.core'
## The following object is masked from 'package:car':
## 
##     bc
## 载入需要的程辑包:spatstat.linnet
## Warning: 程辑包'spatstat.linnet'是用R版本4.1.2 来建造的
## spatstat.linnet 2.3-1
## 
## spatstat 2.3-0       (nickname: 'That's not important right now') 
## For an introduction to spatstat, type 'beginner'
library(broom)
library(tidymodels)
## Warning: 程辑包'tidymodels'是用R版本4.1.2 来建造的
## Registered S3 method overwritten by 'tune':
##   method                   from   
##   required_pkgs.model_spec parsnip
## -- Attaching packages -------------------------------------- tidymodels 0.1.4 --
## v dials        0.0.10     v rsample      0.1.1 
## v infer        1.0.0      v tune         0.1.6 
## v modeldata    0.1.1      v workflows    0.2.4 
## v parsnip      0.1.7      v workflowsets 0.1.0 
## v recipes      0.1.17     v yardstick    0.0.9
## Warning: 程辑包'dials'是用R版本4.1.2 来建造的
## Warning: 程辑包'scales'是用R版本4.1.2 来建造的
## Warning: 程辑包'infer'是用R版本4.1.2 来建造的
## Warning: 程辑包'modeldata'是用R版本4.1.2 来建造的
## Warning: 程辑包'parsnip'是用R版本4.1.2 来建造的
## Warning: 程辑包'recipes'是用R版本4.1.2 来建造的
## Warning: 程辑包'rsample'是用R版本4.1.2 来建造的
## Warning: 程辑包'tune'是用R版本4.1.2 来建造的
## Warning: 程辑包'workflows'是用R版本4.1.2 来建造的
## Warning: 程辑包'workflowsets'是用R版本4.1.2 来建造的
## Warning: 程辑包'yardstick'是用R版本4.1.2 来建造的
## -- Conflicts ----------------------------------------- tidymodels_conflicts() --
## x nlme::collapse()   masks dplyr::collapse()
## x scales::discard()  masks purrr::discard()
## x plotly::filter()   masks dplyr::filter(), stats::filter()
## x recipes::fixed()   masks stringr::fixed()
## x dplyr::lag()       masks stats::lag()
## x tune::parameters() masks dials::parameters(), spatstat.core::parameters()
## x dials::prune()     masks rpart::prune()
## x car::recode()      masks dplyr::recode()
## x car::some()        masks purrr::some()
## x yardstick::spec()  masks readr::spec()
## x recipes::step()    masks stats::step()
## * Use suppressPackageStartupMessages() to eliminate package startup messages
library(usethis)
## Warning: 程辑包'usethis'是用R版本4.1.2 来建造的
library(corrr)
## Warning: 程辑包'corrr'是用R版本4.1.2 来建造的
LondonWardProfiles <- read_csv(here::here('data','out2.csv'), 
                               na = c("", "NA", "n/a"), 
                               col_names = TRUE)
## New names:
## * `` -> ...1
## Rows: 625 Columns: 18
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr  (1): New Code
## dbl (17): ...1, Children_Poverty_Rate, E1, E2, E3, E4, E5, E6, HD1, HD2, L1,...
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Londonwards <- st_read(here::here("data", "statistical-gis-boundaries-london", "statistical-gis-boundaries-london","ESRI", "London_Ward_CityMerged.shp"))
## Reading layer `London_Ward_CityMerged' from data source 
##   `D:\Master\Dissertation\data expplore\1\data\statistical-gis-boundaries-london\statistical-gis-boundaries-london\ESRI\London_Ward_CityMerged.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 625 features and 7 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 503568.2 ymin: 155850.8 xmax: 561957.5 ymax: 200933.9
## Projected CRS: OSGB 1936 / British National Grid
#check all of the columns have been read in correctly
Datatypelist <- LondonWardProfiles %>% 
  summarise_all(class) %>%
  pivot_longer(everything(), 
               names_to="All_variables", 
               values_to="Variable_class")

Datatypelist
## # A tibble: 18 x 2
##    All_variables         Variable_class
##    <chr>                 <chr>         
##  1 ...1                  numeric       
##  2 New Code              character     
##  3 Children_Poverty_Rate numeric       
##  4 E1                    numeric       
##  5 E2                    numeric       
##  6 E3                    numeric       
##  7 E4                    numeric       
##  8 E5                    numeric       
##  9 E6                    numeric       
## 10 HD1                   numeric       
## 11 HD2                   numeric       
## 12 L1                    numeric       
## 13 L2                    numeric       
## 14 O                     numeric       
## 15 P1                    numeric       
## 16 P2                    numeric       
## 17 P3                    numeric       
## 18 Q                     numeric
#merge boundaries and data
LonWardProfiles <- Londonwards%>%
  left_join(.,
            LondonWardProfiles, 
            by = c("GSS_CODE" = "New Code"))
#let's map our dependent variable to see if the join has worked:
tmap_mode("plot")
## tmap mode set to plotting
qtm(LonWardProfiles, 
    fill = "Children_Poverty_Rate", 
    borders = NULL,  
    fill.palette = "Blues")

library(corrr)

Correlation <- LonWardProfiles %>%
  st_drop_geometry()%>%
  dplyr::select(Children_Poverty_Rate,
         E1,
         E2,
         E3,
         E4,
         E5,
         E6,
         HD1,
         HD2,
         L1,
         L2,
         O,
         P1,
         P2,
         P3,
         Q)%>%
    correlate() %>%
  # just focus on Children_Poverty_Rate
  focus(-Children_Poverty_Rate, mirror = TRUE) 
## 
## Correlation method: 'pearson'
## Missing treated using: 'pairwise.complete.obs'
#visualise the correlation matrix
rplot(Correlation)
## Don't know how to automatically pick scale for object of type noquote. Defaulting to continuous.

using the stepwise regression dimension reduced data

Regressiondata2<- LonWardProfiles%>%
  dplyr::select(Children_Poverty_Rate,
       E3,
       E4,
       E6,
       O,
       Q)
model2 <- LonWardProfiles %>%
  lm(Children_Poverty_Rate ~ 
       E3+
       E4+
       E6+
       O+
       Q, data =.)

#show the summary of those outputs
tidy(model2)
## # A tibble: 6 x 5
##   term        estimate std.error statistic   p.value
##   <chr>          <dbl>     <dbl>     <dbl>     <dbl>
## 1 (Intercept)   3.01      1.81        1.66 9.65e-  2
## 2 E3           -0.0601    0.0210     -2.87 4.26e-  3
## 3 E4            0.300     0.0407      7.36 5.72e- 13
## 4 E6            0.0663    0.0123      5.40 9.29e-  8
## 5 O             0.915     0.0132     69.2  2.32e-293
## 6 Q             0.0807    0.0180      4.48 8.94e-  6

all variables are reliable

glance(model2)
## # A tibble: 1 x 12
##   r.squared adj.r.squared sigma statistic p.value    df logLik   AIC   BIC
##       <dbl>         <dbl> <dbl>     <dbl>   <dbl> <dbl>  <dbl> <dbl> <dbl>
## 1     0.978         0.978  1.64     5429.       0     5 -1191. 2397. 2428.
## # ... with 3 more variables: deviance <dbl>, df.residual <int>, nobs <int>
tidy(durbinWatsonTest(model2))
## # A tibble: 1 x 5
##   statistic p.value autocorrelation method             alternative
##       <dbl>   <dbl>           <dbl> <chr>              <chr>      
## 1      1.88   0.112          0.0588 Durbin-Watson Test two.sided

The Durbin-Watson Test result is not significant.

res <- resid(model2)
plot(fitted(model2), res)

The statistic is very close to 2, indicating very small positive autocorrelation.

Because the p-value is less than 0.05, it cannot indicate that there is autocorrelation

droped_London_poverty_merged_file <-     LonWardProfiles %>%
  drop_na()
droped_London_poverty_merged_file_with_residual <- droped_London_poverty_merged_file %>%
  mutate(.,res)
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(droped_London_poverty_merged_file_with_residual) +
  tm_polygons("res",
              palette = "RdYlBu")
## Variable(s) "res" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.

residuals are mostly from -5 to 5, following a normal distribution. Meaning that the regression is feasible.

#calculate the centroids of all Wards in London
coordsW <- droped_London_poverty_merged_file_with_residual%>%
  st_centroid()%>%
  st_geometry()
## Warning in st_centroid.sf(.): st_centroid assumes attributes are constant over
## geometries of x
plot(coordsW)

#Now we need to generate a spatial weights matrix 
#(remember from the lecture a couple of weeks ago). 
#We'll start with a simple binary matrix of queen's case neighbours

LWard_nb <- droped_London_poverty_merged_file_with_residual %>%
  poly2nb(., queen=T)

#or nearest neighbours
knn_wards <-coordsW %>%
  knearneigh(., k=4)

LWard_knn <- knn_wards %>%
  knn2nb()

#plot them
plot(LWard_nb, st_geometry(coordsW), col="red")

plot(LWard_knn, st_geometry(coordsW), col="blue")

test Moran’s I to check spatial autocorrelation

Lward.lw <- LWard_nb %>%
  nb2listw(., style="C")
I_LWard_Global_Density <- droped_London_poverty_merged_file_with_residual %>%
  pull(Children_Poverty_Rate) %>%
  as.vector()%>%
  moran.test(., Lward.lw)

I_LWard_Global_Density
## 
##  Moran I test under randomisation
## 
## data:  .  
## weights: Lward.lw    
## 
## Moran I statistic standard deviate = 25.243, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic       Expectation          Variance 
##      0.5841638337     -0.0016025641      0.0005384643

The statistic value is more close to 1 other than -1. This means there are cluster with similar values.

#create a spatial weights matrix object from these weights
Lward.queens_weight <- LWard_nb %>%
  nb2listw(., style="W")

Lward.knn_4_weight <- LWard_knn %>%
  nb2listw(., style="W")
Queen <- droped_London_poverty_merged_file_with_residual %>%
  st_drop_geometry()%>%
  dplyr::select(res)%>%
  pull()%>%
  moran.test(., Lward.queens_weight)%>%
  tidy()

run a spatially-lagged regression model with a queen’s case weights matrix

library(spatialreg)
## Warning: 程辑包'spatialreg'是用R版本4.1.2 来建造的
## 载入需要的程辑包:Matrix
## 
## 载入程辑包:'Matrix'
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## 
## 载入程辑包:'spatialreg'
## The following objects are masked from 'package:spdep':
## 
##     aple, aple.mc, aple.plot, as.spam.listw, as_dgRMatrix_listw,
##     as_dsCMatrix_I, as_dsCMatrix_IrW, as_dsTMatrix_listw,
##     can.be.simmed, cheb_setup, create_WX, do_ldet, eigen_pre_setup,
##     eigen_setup, eigenw, errorsarlm, get.ClusterOption,
##     get.coresOption, get.mcOption, get.VerboseOption,
##     get.ZeroPolicyOption, GMargminImage, GMerrorsar, griffith_sone,
##     gstsls, Hausman.test, impacts, intImpacts, invIrM, invIrW,
##     Jacobian_W, jacobianSetup, l_max, lagmess, lagsarlm, lextrB,
##     lextrS, lextrW, lmSLX, localAple, LU_prepermutate_setup, LU_setup,
##     Matrix_J_setup, Matrix_setup, mcdet_setup, MCMCsamp, ME, mom_calc,
##     mom_calc_int2, moments_setup, powerWeights, sacsarlm,
##     SE_classic_setup, SE_interp_setup, SE_whichMin_setup,
##     set.ClusterOption, set.coresOption, set.mcOption,
##     set.VerboseOption, set.ZeroPolicyOption, similar.listw, spam_setup,
##     spam_update_setup, SpatialFiltering, spautolm, spBreg_err,
##     spBreg_lag, spBreg_sac, stsls, subgraph_eigenw, trW
slag_dv_model2_queen <- lagsarlm(Children_Poverty_Rate ~ 
       E3+
       E4+
       E6+
       O+
       Q,
               data = droped_London_poverty_merged_file_with_residual, 
               nb2listw(LWard_nb, style="C"), 
               method = "eigen")

#what do the outputs show?
tidy(slag_dv_model2_queen)
## # A tibble: 7 x 5
##   term         estimate std.error statistic  p.value
##   <chr>           <dbl>     <dbl>     <dbl>    <dbl>
## 1 rho          0.000531   0.00768    0.0691 9.45e- 1
## 2 (Intercept)  3.02       1.81       1.67   9.52e- 2
## 3 E3          -0.0604     0.0211    -2.86   4.27e- 3
## 4 E4           0.299      0.0407     7.36   1.85e-13
## 5 E6           0.0663     0.0122     5.42   5.90e- 8
## 6 O            0.915      0.0136    67.1    0       
## 7 Q            0.0804     0.0183     4.39   1.12e- 5
summary(slag_dv_model2_queen)
## 
## Call:lagsarlm(formula = Children_Poverty_Rate ~ E3 + E4 + E6 + O + 
##     Q, data = droped_London_poverty_merged_file_with_residual, 
##     listw = nb2listw(LWard_nb, style = "C"), method = "eigen")
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -9.01098 -0.95980 -0.03594  0.94120  6.95888 
## 
## Type: lag 
## Coefficients: (asymptotic standard errors) 
##              Estimate Std. Error z value  Pr(>|z|)
## (Intercept)  3.021435   1.810558  1.6688  0.095160
## E3          -0.060363   0.021123 -2.8577  0.004267
## E4           0.299345   0.040677  7.3590 1.854e-13
## E6           0.066251   0.012220  5.4217 5.903e-08
## O            0.914510   0.013621 67.1379 < 2.2e-16
## Q            0.080438   0.018309  4.3934 1.116e-05
## 
## Rho: 0.00053079, LR test value: 0.0048011, p-value: 0.94476
## Asymptotic standard error: 0.0076811
##     z-value: 0.069104, p-value: 0.94491
## Wald statistic: 0.0047754, p-value: 0.94491
## 
## Log likelihood: -1191.468 for lag model
## ML residual variance (sigma squared): 2.6507, (sigma: 1.6281)
## Number of observations: 625 
## Number of parameters estimated: 8 
## AIC: 2398.9, (AIC for lm: 2396.9)
## LM test for residual autocorrelation
## test value: 10.789, p-value: 0.0010208
glance(slag_dv_model2_queen)
## # A tibble: 1 x 6
##   r.squared   AIC   BIC deviance logLik  nobs
##       <dbl> <dbl> <dbl>    <dbl>  <dbl> <int>
## 1     0.978 2399. 2434.    1657. -1191.   625

#run a spatially-lagged regression model # Using the 4 nearest neighbours instead of just considering all adjcent zones in the spatial weights matrix

#run a spatially-lagged regression model
slag_dv_model2_knn4 <- lagsarlm(Children_Poverty_Rate ~ 
       E3+
       E4+
       E6+
       O+
       Q, 
          data = droped_London_poverty_merged_file_with_residual, 
               nb2listw(LWard_knn, 
                        style="C"), 
               method = "eigen")

#what do the outputs show?
tidy(slag_dv_model2_knn4)
## # A tibble: 7 x 5
##   term        estimate std.error statistic  p.value
##   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
## 1 rho           0.0234    0.0125      1.87 6.10e- 2
## 2 (Intercept)   3.35      1.81        1.86 6.34e- 2
## 3 E3           -0.0662    0.0211     -3.13 1.74e- 3
## 4 E4            0.289     0.0409      7.06 1.66e-12
## 5 E6            0.0642    0.0122      5.25 1.55e- 7
## 6 O             0.903     0.0144     62.7  0       
## 7 Q             0.0731    0.0184      3.98 6.87e- 5
glance(slag_dv_model2_knn4)
## # A tibble: 1 x 6
##   r.squared   AIC   BIC deviance logLik  nobs
##       <dbl> <dbl> <dbl>    <dbl>  <dbl> <int>
## 1     0.978 2395. 2431.    1647. -1190.   625

The Spatial Error Model with queen’s case wight matrix

sem_model2 <- errorsarlm(Children_Poverty_Rate ~ 
       E3+
       E4+
       E6+
       O+
       Q,  
               data = droped_London_poverty_merged_file_with_residual,
               nb2listw(LWard_nb, style="C"), 
               method = "eigen")

tidy(sem_model2)
## # A tibble: 7 x 5
##   term        estimate std.error statistic  p.value
##   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
## 1 (Intercept)   2.05      1.96        1.05 2.96e- 1
## 2 E3           -0.0495    0.0229     -2.17 3.03e- 2
## 3 E4            0.322     0.0455      7.08 1.47e-12
## 4 E6            0.0695    0.0129      5.36 8.15e- 8
## 5 O             0.907     0.0143     63.5  0       
## 6 Q             0.0846    0.0196      4.31 1.60e- 5
## 7 lambda        0.196     0.0628      3.12 1.83e- 3
glance(sem_model2)
## # A tibble: 1 x 6
##   r.squared   AIC   BIC deviance logLik  nobs
##       <dbl> <dbl> <dbl>    <dbl>  <dbl> <int>
## 1     0.978 2389. 2425.    1620. -1187.   625
sem_model2_2 <- errorsarlm(Children_Poverty_Rate ~ 
       E3+
       E4+
       E6+
       O+
       Q,  
               data = droped_London_poverty_merged_file_with_residual,
               nb2listw(LWard_knn, style="C"), 
               method = "eigen")

tidy(sem_model2_2)
## # A tibble: 7 x 5
##   term        estimate std.error statistic  p.value
##   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
## 1 (Intercept)   1.69      1.96       0.861 3.89e- 1
## 2 E3           -0.0446    0.0228    -1.95  5.08e- 2
## 3 E4            0.325     0.0453     7.16  7.87e-13
## 4 E6            0.0692    0.0130     5.33  9.72e- 8
## 5 O             0.907     0.0142    63.7   0       
## 6 Q             0.0847    0.0198     4.27  1.96e- 5
## 7 lambda        0.193     0.0553     3.49  4.84e- 4
glance(sem_model2_2)
## # A tibble: 1 x 6
##   r.squared   AIC   BIC deviance logLik  nobs
##       <dbl> <dbl> <dbl>    <dbl>  <dbl> <int>
## 1     0.978 2387. 2423.    1613. -1186.   625

run a lag model if you identify spatial autocorrelation in the dependent variable (closer spatial units have similar values) with Moran’s I.

The Lagrange multiple tests

Test with queen’s case

library(spdep)

Lward.queens_weight_ROW <- LWard_nb %>%
  nb2listw(., style="W")

lm.LMtests(model2, Lward.queens_weight_ROW, test = c("LMerr","LMlag","RLMerr","RLMlag","SARMA"))
## 
##  Lagrange multiplier diagnostics for spatial dependence
## 
## data:  
## model: lm(formula = Children_Poverty_Rate ~ E3 + E4 + E6 + O + Q, data
## = .)
## weights: Lward.queens_weight_ROW
## 
## LMerr = 8.9109, df = 1, p-value = 0.002835
## 
## 
##  Lagrange multiplier diagnostics for spatial dependence
## 
## data:  
## model: lm(formula = Children_Poverty_Rate ~ E3 + E4 + E6 + O + Q, data
## = .)
## weights: Lward.queens_weight_ROW
## 
## LMlag = 0.77683, df = 1, p-value = 0.3781
## 
## 
##  Lagrange multiplier diagnostics for spatial dependence
## 
## data:  
## model: lm(formula = Children_Poverty_Rate ~ E3 + E4 + E6 + O + Q, data
## = .)
## weights: Lward.queens_weight_ROW
## 
## RLMerr = 8.2233, df = 1, p-value = 0.004136
## 
## 
##  Lagrange multiplier diagnostics for spatial dependence
## 
## data:  
## model: lm(formula = Children_Poverty_Rate ~ E3 + E4 + E6 + O + Q, data
## = .)
## weights: Lward.queens_weight_ROW
## 
## RLMlag = 0.089295, df = 1, p-value = 0.7651
## 
## 
##  Lagrange multiplier diagnostics for spatial dependence
## 
## data:  
## model: lm(formula = Children_Poverty_Rate ~ E3 + E4 + E6 + O + Q, data
## = .)
## weights: Lward.queens_weight_ROW
## 
## SARMA = 9.0002, df = 2, p-value = 0.01111

Test with KNN

Lward.knn_weight_ROW <- LWard_knn %>%
  nb2listw(., style="W")

lm.LMtests(model2, Lward.knn_weight_ROW, test = c("LMerr","LMlag"))
## 
##  Lagrange multiplier diagnostics for spatial dependence
## 
## data:  
## model: lm(formula = Children_Poverty_Rate ~ E3 + E4 + E6 + O + Q, data
## = .)
## weights: Lward.knn_weight_ROW
## 
## LMerr = 12.323, df = 1, p-value = 0.0004475
## 
## 
##  Lagrange multiplier diagnostics for spatial dependence
## 
## data:  
## model: lm(formula = Children_Poverty_Rate ~ E3 + E4 + E6 + O + Q, data
## = .)
## weights: Lward.knn_weight_ROW
## 
## LMlag = 3.5595, df = 1, p-value = 0.05921